import java.io.File;
import java.util.LinkedList;

public class AIM {
	public static final String DEFAULT_INDEX_FILENAME = "AIM.out";
	
	private String [] filenames;
	private final CorpusIndexRWFactory DEFAULT_AIM_INDEX_RW_FACTORY = new SAMZIndexRWFactory(); 
	private String variableBreaks;
	private String stopFile;
	private LinkedList phraseHistograms;
	private FileIndex fileIndex;

	/**Entry point for the application, builds an index from a corpus
	 * @param args [optional] parameters of -b and -s followed by a string, followed by strings of file paths 
	 */
	public static void main(String[] args) throws AIMException{
		// TODO Auto-generated method stub
		String stopFile=null;
		String variableBreaks=WordTokenizer.VARIABLE_BREAKS;;
		String []fileNames = null;
		int numArgs = args.length;
		int numFiles = numArgs;
		try
		{
			if (args.length>=2)
			{
				if(args[0].equals("-b"))
				{
					variableBreaks=args[1];
					numFiles-=2;
				}
				else if(args[0].equals("-s"))
				{
					stopFile=args[1];
					numFiles-=2;
				}
				if (numFiles==numArgs-2 && numArgs>=4)
				{
					if(args[2].equals("-b")){
						if(variableBreaks!=args[1])
						{
							variableBreaks=args[3];
							numFiles-=2;
	
						}
					}
					else if(args[2].equals("-s"))
					{
						if(stopFile==null)
						{
							stopFile=args[3];
							numFiles-=2;
						}						
					}
				}
			}
			if(numFiles==0)throw new AIMException(AIMException.NO_FILES);
			
			fileNames=new String[numFiles];
			
			System.arraycopy(args,args.length-numFiles,fileNames,0,fileNames.length);
			AIM aim = new AIM(fileNames,stopFile,variableBreaks);
			aim.indexToFile(DEFAULT_INDEX_FILENAME);
		}catch (AIMException e)
		{
			System.out.println(e.getMessage());
			System.exit(-1);
		}catch (Exception e)
		{
			System.out.println("ERROR: program terminated unexpectedly");
			e.printStackTrace();
			System.exit(-1);			
		}
		return;
	}

	/**
	 * Creates a new AIM object with the corpus file paths, stop word file path, and variable breaks
	 * @param filenames array of file paths in the corpus
	 * @param stopFile path of stop file
	 * @param variableBreaks
	 */
	public AIM(String [] filenames,String stopFile,String variableBreaks)
	{
		this.filenames=filenames;
		for (int i=0;i<filenames.length;i++)
		{
			File file = new File(filenames[i]);
			filenames[i]=file.getAbsolutePath();
			
		}
		this.variableBreaks=variableBreaks;
		this.stopFile=stopFile;
		phraseHistograms=null;
		fileIndex=null;
	}
	/**
	 * Cycles through all the files in the corpus, creates a new histo for each and extracts the words
	 *
	 */
	public void buildHistogram () throws AIMException
	{
		if(filenames!=null)
		{
			phraseHistograms=new LinkedList();
			for(int i=0;i<filenames.length;i++)
			{
				Histo histo=new Histo(1,stopFile,1);
				if(histo.buildHisto(filenames[i], variableBreaks))
				{
					phraseHistograms.add(histo.getPhraseHistogram());
				}
			}
		}
	}
	/**
	 * Makes the call to its FileIndex object to write the index to the specified file
	 * @param outfile path name of the file to be written
	 * @throws Exception
	 */
	public void indexToFile(String outfile) throws Exception
	{
		if(fileIndex==null)
			generateIndex();
		fileIndex.exportFile(outfile);
	}
	
	/**
	 * Generates a FileIndex from the PhraseHistogram list 
	 * @throws Exception
	 */
	public void generateIndex() throws Exception
	{
		if(phraseHistograms==null)
			buildHistogram();
		fileIndex= new FileIndex(this.DEFAULT_AIM_INDEX_RW_FACTORY);
		Object [] temp=new Object [phraseHistograms.size()];
		phraseHistograms.toArray(temp);
		PhraseHistogram [] phraseHistogramArray = new PhraseHistogram[temp.length];
		for (int i=0;i<temp.length;i++)
		{
			phraseHistogramArray[i]=(PhraseHistogram)temp[i];
		}
		
		try
		{
			fileIndex.buildFileIndex(phraseHistogramArray);
			fileIndex.setCommandLineArgumentsInfo(this.stopFile, this.variableBreaks);
		}catch(Exception e){throw e;}
		
	}	
}
